package com.example.naodian.service.impl;

import com.example.naodian.domain.po.EegDataBlock;
import com.example.naodian.domain.po.Warning;
import com.example.naodian.mapper.WarningMapper;
import com.example.naodian.service.IWarningService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.naodian.websocket.WebSocketServer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
 * <p>
 * 预警信息表 服务实现类
 * </p>
 *
 * @author icewx
 * @since 2025-04-17
 */
@Service
@RequiredArgsConstructor
public class WarningServiceImpl extends ServiceImpl<WarningMapper, Warning> implements IWarningService {
    private final WebSocketServer webSocketServer;

    /**
     * 根据预警类型或时间搜索预警信息
     * @param type
     * @param date
     * @return 信息集合
     */
    @Override
    public List<Warning> getListByTypeAndTime(Integer type, LocalDate date) {
        //创建一个map集合(使用 Map 集合是为了封装查询条件，简化参数传递，并适配底层数据库查询接口的需求。)
        Map map = new HashMap<>();
        if (type != null){
            map.put("type", type);
        }
        //将传入的时间取当天的0点和23:99
        if (date != null){
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            map.put("begin",beginTime);
            map.put("end",endTime);
        }

        //根据预警类型和时间搜索预警信息
        List<Warning> warningList = baseMapper.getListByTypeAndTime(map);
        //使用循环设置EegDataBlockList中的值
        for (Warning warning : warningList) {
           warning.setEegDataBlockList(baseMapper.getEegDataBlockById(warning.getId()));
        }
        return warningList;
    }

    /**
     * 根据预警id查询相关点位信息
     * @param id
     * @return
     */
    @Override
    public List<EegDataBlock> getEegDataBlockById(Long id) {
        List<EegDataBlock> eegDataBlockList = baseMapper.getEegDataBlockById(id);
//        if (eegDataBlockList == null || eegDataBlockList.isEmpty()) {
//            throw new BaseException("没有该预警!");
//        }
        return eegDataBlockList;
    }

    /**
     * 插入预警信息
     * @param warning
     */
    @Override
    public void insert(Warning warning) {
        save(warning);
    }
}
